#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qdebug.h>
#include <qprocess.h>
#include<QFileDialog>
#include <QPalette>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    QPalette palette = this->palette();
    palette.setBrush(QPalette::Window,QBrush(QPixmap(":/imgs/backGround.jpg")));
    this->setPalette(palette);

    m_timer1.setInterval(100);

    connect(&m_timer1,SIGNAL(timeout()),this,SLOT(onTimer1Out()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onTimer1Out()
{
    ui->progressBar->setValue(ui->progressBar->value()+QRandomGenerator(QTime::currentTime().second()).bounded(1000)%4+1);
    m_proccesstime+=100;
    ui->textBrowser->append("处理进度:"+QString::number(ui->progressBar->value())+"%");
    if(ui->progressBar->value()>=100){
        ui->textBrowser->append("处理完成.");
        m_timer1.stop();
        ui->progressBar->setValue(100);
        ui->label_2->setPixmap(QPixmap(":/imgs/fg1.png").scaled(ui->label_2->size()));
        ui->label_3->setPixmap(QPixmap(":/imgs/mask1.png").scaled(ui->label_3->size()));
        ;
        ui->textBrowser->append("文件路径:"+m_filePath);
        ui->textBrowser->append("处理时间:"+QString::number(m_proccesstime/1000)+"s");
    }
}

void MainWindow::on_pushButton_clicked()
{
    QString filePath;
    filePath=QFileDialog::getOpenFileName(0,"请选择需要处理的图片",QDir::homePath(),"图片文件(*.png *.jpg)");
    if(!filePath.isEmpty()){
        ui->label->setPixmap(QPixmap(filePath).scaled(ui->label->size()));
        m_filePath=filePath;
        ui->textBrowser->append("已选中图片:"+filePath);
    }else{
        ui->textBrowser->append("未选择图片.");
    }
    // ui->progressBar->setValue(0);
    // m_file1
    // ui->label_2->setPixmap(QPixmap());


    // QProcess myProcess(this);
    // QStringList arguments;

    // while(1)
    // {
    // QString s2 = QFileDialog::getOpenFileName(this, "选择要播放的文件","../profile"," 所有文件 (*.*);; ");

    // arguments<<s2;//传递到exe的参数
    // //qDebug()<<arguments[0];
    //   if(s2==NULL)
    //   {
    //       break;
    //   }

    // }
    // if(arguments[0]==NULL)
    // {
    //     qDebug("没有要处理的图片");
    // }
    // else
    // {
    //     QString tmp=arguments.at(0);
    //     qDebug()<<tmp;
    //     myProcess.startDetached("C:/Users/86152/Desktop/04/1/cmake-build-release-visual-studio/sod_infer.exe",arguments);//D:\qttext\untitled\examples\MinSizeRel
    // }
}

void MainWindow::OnFinishProc(int, QProcess::ExitStatus)
{
    QProcess * proc = (QProcess *)sender();
    delete proc;
}



void MainWindow::on_pushButton_2_clicked()
{
    if(ui->label->pixmap()==NULL){
        QMessageBox::warning(0,"警告","请先选择图片再执行此操作.");
        return;
    }

    if(m_timer1.isActive()){
        ui->textBrowser->append("已停止处理图片.");
        m_timer1.stop();
    }else{
        ui->progressBar->setValue(0);
        m_timer1.start();
        m_proccesstime=0;
        ui->textBrowser->append("开始处理图片...");
        ui->label_2->setPixmap(QPixmap());
        ui->label_3->setPixmap(QPixmap());
    }


    // QProcess myProcess(this);
    // QStringList arguments;

    // while(1)
    // {
    // QString s2 = QFileDialog::getOpenFileName(this, "选择要播放的文件","../profile"," 所有文件 (*.*);; ");

    // arguments<<s2;//传递到exe的参数
    // //qDebug()<<arguments[0];
    //   if(s2==NULL)
    //   {
    //       break;
    //   }

    // }
    // if(arguments[0]==NULL)
    // {
    //     qDebug("没有要处理的图片");
    // }
    // else
    // {
    //     QString tmp=arguments.at(0);
    //     qDebug()<<tmp;
    //     myProcess.startDetached("C:/Users/86152/Desktop/04/1/cmake-build-release-visual-studio/sod_infer.exe",arguments);//D:\qttext\untitled\examples\MinSizeRel
    // }
}

void MainWindow::on_pushButton_3_clicked()
{
    QProcess pro(this);
    pro.startDetached("../cmake-build-release-visual-studio/sod_infer.exe",QStringList());
}

